<template>
  <div>
    <div v-if="parentFlagShow" class="area-header">
      <span class="area-header-title">{{ $t('基金分配-出资人分配汇总') }}</span>
    </div>
    <el-table v-loading="loading" :data="fundInvestorFpDetailsList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('出资人名称')" align="center" prop="investorName" min-width="180" show-overflow-tooltip />
      <el-table-column :label="$t('出资人类型')" align="center" prop="investorsType" :formatter="investorsTypeOptionsFormat" min-width="140" show-overflow-tooltip />
      <el-table-column :label="$t('应分配金额') + $t('金额单位')" align="center" min-width="120" prop="totalAmount" @change="calAfterTaxFpAmount(scope.row, scope.$index)">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.totalAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('税后实际分配金额') + $t('金额单位')" align="center" min-width="170">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.afterTaxFpAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('本金') + $t('金额单位')" align="center" prop="principalCost" min-width="120">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.principalCost, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('Hurdle') + $t('金额单位')" align="center" prop="priorityReturn" min-width="120">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.hurdleTheory, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('股息红利') + $t('金额单位')" align="center" prop="dividends" min-width="120">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.dividends, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('追赶条款') + $t('金额单位')" align="center" min-width="120" prop="chaseTerms">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.chaseTerms, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('剩余分配LP') + $t('金额单位')" align="center" min-width="120" prop="surplusLp">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.surplusLp, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('剩余分配GP') + $t('金额单位')" align="center" min-width="120" prop="surplusGp">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.surplusGp, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('代扣税费') + $t('金额单位')" align="center" min-width="100">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.taxeFee, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('操作')"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['fund:fp:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="openInvestorFpDetails(scope.row)"
          >{{ $t('出资人分配明细') }}
          </el-button>
          <el-button
            v-if="!otherBtnHide"
            v-hasPermi="['fund:fp:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 基金分配明细对话框, 出资人当前这次分配的详情数据 -->
    <el-dialog
      :title="detailsTitle"
      :visible.sync="detailsOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogView"
      append-to-body
    >
      <fund-investor-fp-details-list ref="fundInvestorFpDetailsList" :other-btn-hide="otherBtnHide" />
    </el-dialog>
  </div>
</template>

<script>
import { delFundFpDetailsByInvestorId, listDetailGroupInvestor } from '@/api/fund/fp/fundFpDetails'
import fundInvestorFpDetailsList from './fundInvestorFpDetailsList.vue'
import { ruoyiFloat } from '@/utils/ruoyi'

export default {
  components: {
    fundInvestorFpDetailsList
  },
  props: {
    topDataList: {
      type: Array,
      default: () => []
    },
    parentFlagShow: {
      type: Boolean,
      default: () => true
    },
    otherBtnHide: {
      type: Boolean,
      default: () => false
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 查看详情弹出层
      detailsOpen: false,
      // 查看详情弹出层标题
      detailsTitle: '',
      // 基金分配-分配详情表格数据
      fundInvestorFpDetailsList: [],
      investorsTypeOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        fpId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  watch: {
    topDataList: {
      handler(val) {
        if (val) {
          this.fundInvestorFpDetailsList = val
        }
      },
      deep: true,
      immediate: true
    },
    fundInvestorFpDetailsList: {
      handler(val) {
        if (val) {
          this.$emit('updateParentData', val)
        }
      }
    }
  },
  created() {
    // 出资人类型字典
    this.getDicts('fund_investor_type').then(response => {
      response.data.forEach(item => {
        this.investorsTypeOptions.push(item)
      })
    })
  },
  methods: {
    listDetailGroupInvestor(row) {
      this.loading = true
      if (row.fpId !== undefined && row.fpId !== null) {
        this.queryParams.fpId = row.fpId
      } else {
        this.queryParams.fpId = row.id
      }
      listDetailGroupInvestor(this.queryParams).then(response => {
        this.fundInvestorFpDetailsList = response.rows
        this.fundInvestorFpDetailsList.forEach(item => {
          var newVar = this.formatMoney(item.principalCost) +
            this.formatMoney(item.surplusLp) +
            this.formatMoney(item.hurdleTheory) +
            this.formatMoney(item.dividends) +
            this.formatMoney(item.chaseTerms) +
            this.formatMoney(item.surplusGp)
          item.totalAmount = this.formatMoney(newVar)
          row.afterTaxFpAmount = this.formatMoney(item.totalAmount) - this.formatMoney(row.item)
        })
        this.loading = false
      })
    },
    // 计算税后实际分配金额（应分配金额-代扣税费）
    calAfterTaxFpAmount(row, index) {
      var newVar = this.formatMoney(row.totalAmount) - this.formatMoney(row.taxeFee)
      this.form.fundInvestorFpDetailsList[index].afterTaxFpAmount = this.formatMoney(newVar)
    },
    formatMoney(val) {
      if (val && val !== '' && !isNaN(parseFloat(val))) {
        return ruoyiFloat().formatNum(val)
      }
      return 0
    },
    // 打开出资人分配明细页面
    openInvestorFpDetails(row) {
      this.detailsOpen = true
      this.detailsTitle = this.$t('基金分配-出资人详情')
      this.$nextTick(() => {
        this.$refs.fundInvestorFpDetailsList.getfundInvestorFpDetails(row)
      })
    },
    // 出资人类型
    investorsTypeOptionsFormat(row) {
      return this.selectDictLabel(this.investorsTypeOptions, row.investorsType)
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fundId: undefined,
        fpId: undefined,
        investorId: undefined,
        investorName: undefined,
        userName: undefined,
        investorsType: undefined,
        assignItems: undefined,
        fpTime: undefined,
        principalCost: undefined,
        priorityReturn: undefined,
        lpCapitalRatio: undefined,
        clauseTheory: undefined,
        surplusLp: undefined,
        surplusGp: undefined,
        fpSourceId: undefined,
        dealId: undefined,
        dealName: undefined,
        dealBkId: undefined,
        chaseTerms: undefined,
        balanceFp: undefined,
        taxeFee: undefined,
        allCapitalRatio: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return delFundFpDetailsByInvestorId(row)
      }).then(() => {
        this.listDetailGroupInvestor(row)
        this.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>
